/**
 * @author Jason Pratt
 */

package edu.cmu.cs.stage3.alice.authoringtool.viewcontroller;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;
import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;
import edu.cmu.cs.stage3.alice.authoringtool.datatransfer.TransferableFactory;

public class CallToUserDefinedQuestionPrototypeDnDPanel extends edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel implements edu.cmu.cs.stage3.alice.authoringtool.util.GUIElement, edu.cmu.cs.stage3.alice.authoringtool.util.Releasable {

 protected edu.cmu.cs.stage3.alice.authoringtool.util.CallToUserDefinedQuestionPrototype callToUserDefinedQuestionPrototype;
 protected edu.cmu.cs.stage3.alice.authoringtool.util.GroupingPanel subPanel = new edu.cmu.cs.stage3.alice.authoringtool.util.GroupingPanel();
 protected java.awt.GridBagConstraints constraints = new java.awt.GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 );
 protected RefreshListener refreshListener = new RefreshListener();
 protected boolean nameEditorShouldBeInvoked = false;
 protected MouseListener mouseListener = new MouseListener();
 protected ElementNamePropertyViewController nameViewController;

 public CallToUserDefinedQuestionPrototypeDnDPanel() {
  this.setBackground( AuthoringToolResources.getColor( "userDefinedQuestion" ) );
  subPanel.setLayout( new java.awt.GridBagLayout() );
  subPanel.setOpaque( false );
  subPanel.setBorder( null );
  this.add( subPanel, java.awt.BorderLayout.CENTER );
  this.addDragSourceComponent( subPanel );
 }

 public void set( edu.cmu.cs.stage3.alice.authoringtool.util.CallToUserDefinedQuestionPrototype callToUserDefinedQuestionPrototype ) {
  clean();

  this.callToUserDefinedQuestionPrototype = callToUserDefinedQuestionPrototype;
  this.setTransferable( TransferableFactory.createTransferable( callToUserDefinedQuestionPrototype ) );
  startListening();
  refreshGUI();
 }

 protected void startListening() {
  if( callToUserDefinedQuestionPrototype != null ) {
   callToUserDefinedQuestionPrototype.startListening();
   edu.cmu.cs.stage3.alice.core.question.userdefined.UserDefinedQuestion question = callToUserDefinedQuestionPrototype.getActualQuestion();
   if( question != null ) {
    question.requiredFormalParameters.addObjectArrayPropertyListener( refreshListener );
    question.keywordFormalParameters.addObjectArrayPropertyListener( refreshListener );
    Object[] variables = question.requiredFormalParameters.getArrayValue();
    //LOB for (Object variable : variables)
    int t_tam = variables!=null ? variables.length : 0;
    for (int i_i=0; i_i<t_tam; i_i++) {
     ((edu.cmu.cs.stage3.alice.core.Variable)variables[i_i]).name.addPropertyListener( refreshListener ); // (...)variable).name.addPropertyListener( refreshListener );
    }
    variables = question.keywordFormalParameters.getArrayValue();
    //LOB for (Object variable : variables)
    t_tam = variables!=null ? variables.length : 0;
    for (int i_i=0; i_i<t_tam; i_i++) {
     ((edu.cmu.cs.stage3.alice.core.Variable)variables[i_i]).name.addPropertyListener( refreshListener ); // (...)variable).name.addPropertyListener( refreshListener );
    }
    question.name.addPropertyListener( refreshListener );
   }
  }
 }

 protected void stopListening() {
  if( callToUserDefinedQuestionPrototype != null ) {
   callToUserDefinedQuestionPrototype.stopListening();
   edu.cmu.cs.stage3.alice.core.question.userdefined.UserDefinedQuestion question = callToUserDefinedQuestionPrototype.getActualQuestion();
   if( question != null ) {
    question.requiredFormalParameters.removeObjectArrayPropertyListener( refreshListener );
    question.keywordFormalParameters.removeObjectArrayPropertyListener( refreshListener );
    Object[] variables = question.requiredFormalParameters.getArrayValue();
    //LOB for (Object variable : variables)
    int t_tam = variables!=null ? variables.length : 0;
    for (int i_i=0; i_i<t_tam; i_i++) {
     ((edu.cmu.cs.stage3.alice.core.Variable)variables[i_i]).name.removePropertyListener( refreshListener ); //L (...)variable).name.removePropertyListener( refreshListener );
    }
    variables = question.keywordFormalParameters.getArrayValue();
    //LOB for (Object variable : variables)
    t_tam = variables!=null ? variables.length : 0;
    for (int i_i=0; i_i<t_tam; i_i++) {
     ((edu.cmu.cs.stage3.alice.core.Variable)variables[i_i]).name.removePropertyListener( refreshListener ); //L (...)variable).name.removePropertyListener( refreshListener );
    }
    question.name.removePropertyListener( refreshListener );
   }
  }
 }

 public void goToSleep() {
  stopListening();
 }

 public void wakeUp() {
  startListening();
 }

 public void clean() {
  stopListening();
  this.callToUserDefinedQuestionPrototype = null;
  this.setTransferable( null );
  refreshGUI();
 }

 public void die() {
  clean();
  subPanel.removeAll();
  removeAll();
 }

 //Rem @Override
 public void release() {
  edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.releaseGUI( this );
 }

 

 public void refreshGUI() {
  if( nameViewController != null ) {
   removeDragSourceComponent( nameViewController );
  }
  subPanel.removeAll();

  if( callToUserDefinedQuestionPrototype != null ) {
   edu.cmu.cs.stage3.alice.core.question.userdefined.UserDefinedQuestion question = callToUserDefinedQuestionPrototype.getActualQuestion();
   constraints.gridx = 0;
   nameViewController = null;
   if( question != null ) {
    nameViewController = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getElementNamePropertyViewController( question );
    nameViewController.setBorder( null );
    nameViewController.setOpaque( false );
    subPanel.add( nameViewController, constraints );
    addDragSourceComponent( nameViewController );
   } else {
    subPanel.add( new javax.swing.JLabel( "<null question>" ), constraints );
   }
   constraints.gridx++;
   subPanel.add( javax.swing.Box.createHorizontalStrut( 6 ), constraints );
   constraints.gridx++;
   if( (question != null) && (question.requiredFormalParameters.size() > 0) ) {
    Object[] variables = question.requiredFormalParameters.getArrayValue();
    //LOB for (Object variable : variables) //TODO: include type info?
    int t_tam = variables!=null ? variables.length : 0;
    for (int i_i=0; i_i<t_tam; i_i++) {
     addTile( ((edu.cmu.cs.stage3.alice.core.Variable)variables[i_i]).name.getStringValue() ); //L (...)variable).name.getStringValue() );
     constraints.gridx++;
    }
   }

   //TODO: keyword parameters
   if( nameEditorShouldBeInvoked && (nameViewController != null) ) {
    nameViewController.editValue();
    nameEditorShouldBeInvoked = false;
   }
  } else {
   subPanel.add( new javax.swing.JLabel( "<null callToUserDefinedQuestionPrototype>" ), constraints );
  }

  revalidate();
  repaint();
 }

 public void addTile( String text ) {
  edu.cmu.cs.stage3.alice.authoringtool.util.GroupingPanel tilePanel = new edu.cmu.cs.stage3.alice.authoringtool.util.GroupingPanel();
  tilePanel.setLayout( new java.awt.BorderLayout() );
  tilePanel.setBackground( AuthoringToolResources.getColor( "prototypeParameter" ) );
  javax.swing.JLabel tileLabel = new javax.swing.JLabel( text );
  tileLabel.setBorder( javax.swing.BorderFactory.createEmptyBorder( 0, 2, 0, 2 ) );
  tilePanel.add( tileLabel, java.awt.BorderLayout.CENTER );
  subPanel.add( tilePanel, constraints );
  //  tilePanel.addMouseListener( mouseListener );
  this.addDragSourceComponent( tilePanel );
 }

 class MouseListener extends edu.cmu.cs.stage3.alice.authoringtool.util.CustomMouseAdapter {
  //Rem @Override
  public void singleClickResponse( java.awt.event.MouseEvent ev ) {
   AuthoringTool.getInstance().editObject( callToUserDefinedQuestionPrototype.getActualQuestion(), CallToUserDefinedQuestionPrototypeDnDPanel.this );
  }
 }

 class RefreshListener implements edu.cmu.cs.stage3.alice.core.event.PropertyListener, edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyListener {
  public void propertyChanging( edu.cmu.cs.stage3.alice.core.event.PropertyEvent ev ) {}
  public void propertyChanged( edu.cmu.cs.stage3.alice.core.event.PropertyEvent ev ) {
   CallToUserDefinedQuestionPrototypeDnDPanel.this.refreshGUI();
  }
  public void objectArrayPropertyChanging( edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyEvent ev ) {}
  public void objectArrayPropertyChanged( edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyEvent ev ) {
   if( ev.getChangeType() == edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyEvent.ITEM_INSERTED ) {
    edu.cmu.cs.stage3.alice.core.Variable variable = (edu.cmu.cs.stage3.alice.core.Variable)ev.getItem();
    variable.name.addPropertyListener( this );
   } else if( ev.getChangeType() == edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyEvent.ITEM_REMOVED ) {
    edu.cmu.cs.stage3.alice.core.Variable variable = (edu.cmu.cs.stage3.alice.core.Variable)ev.getItem();
    variable.name.removePropertyListener( this );
   }
   CallToUserDefinedQuestionPrototypeDnDPanel.this.refreshGUI();
  }
 };

}
